var express = require('express');
var path = require('path');
var _ = require('underscore');
// var mongoose = require('mongoose');
// var Movie = require('./models/movie')
var serveStatic = require('serve-static');
var bodyParser = require('body-parser');
var port = process.env.PORT || 3001;
var app = express();
// mongoose.Promise = require('bluebird');
// mongoose.connect('mongodb://localhost:27017/imooc', {useMongoClient:true});

app.set('views','./views/pages');
app.set('view engine','jade');
app.use(bodyParser.json() );
app.use(bodyParser.urlencoded({extended: true}));
app.use(serveStatic('public'));
app.locals.moment = require('moment');
app.listen(port);
console.log('Setver is running at ' + port);

//index
// app.get('/',(req,res)=>{
//   Movie.fetch(function(err,movies) {
//     if(err){
//       console.log(err);
//     }
//     res.render('index',{
//       title:'MOMO 首页',
//       movies:movies
//     })
//   })
// })
app.get('/', function(req, res){
  res.render('index', {
    title: 'imooc 首页',
    movies: [{
      title: '机械战警',
      _id: 1,
      poster: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1514040326143&di=c453a52cb7a96042416ad22f0bfd380a&imgtype=0&src=http%3A%2F%2Fent.southcn.com%2F8%2Fimages%2Fattachement%2Fjpg%2Fsite4%2F20140307%2F60%2F6146180416608347392.jpg'
    },{
      title: '机械战警',
      _id: 2,
      poster: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1514040326143&di=c453a52cb7a96042416ad22f0bfd380a&imgtype=0&src=http%3A%2F%2Fent.southcn.com%2F8%2Fimages%2Fattachement%2Fjpg%2Fsite4%2F20140307%2F60%2F6146180416608347392.jpg'
    },{
      title: '机械战警',
      _id: 3,
      poster: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1514040326143&di=c453a52cb7a96042416ad22f0bfd380a&imgtype=0&src=http%3A%2F%2Fent.southcn.com%2F8%2Fimages%2Fattachement%2Fjpg%2Fsite4%2F20140307%2F60%2F6146180416608347392.jpg'
    },{
      title: '机械战警',
      _id: 4,
      poster: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1514040326143&di=c453a52cb7a96042416ad22f0bfd380a&imgtype=0&src=http%3A%2F%2Fent.southcn.com%2F8%2Fimages%2Fattachement%2Fjpg%2Fsite4%2F20140307%2F60%2F6146180416608347392.jpg'
    },{
      title: '机械战警',
      _id: 5,
      poster: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1514040326143&di=c453a52cb7a96042416ad22f0bfd380a&imgtype=0&src=http%3A%2F%2Fent.southcn.com%2F8%2Fimages%2Fattachement%2Fjpg%2Fsite4%2F20140307%2F60%2F6146180416608347392.jpg'
    },{
      title: '机械战警',
      _id: 6,
      poster: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1514040326143&di=c453a52cb7a96042416ad22f0bfd380a&imgtype=0&src=http%3A%2F%2Fent.southcn.com%2F8%2Fimages%2Fattachement%2Fjpg%2Fsite4%2F20140307%2F60%2F6146180416608347392.jpg'
    }]
  })
})

//detail
// app.get('/movie/:id',(req,res)=>{
//   console.log('enter detail');
//   var id = req.params.id;
//   Movie.findById(id,function(err,movie) {
//     console.log(movie)
//     if(err) {
//       console.log(err);
//     }
//     res.render('detail',{
//       title:'MOMO'+movie.title,
//       movie:movie
//     })
//   })
// })

//admin
// app.get('/admin/movie',(req,res)=>{
//   console.log('enter admin');
//   res.render('admin',{
//       title:'MOMO 后台',
//       movie:{
//         doctor:'',
//         country:'',
//         title:'',
//         year:'',
//         poster:'',
//         language:'',
//         flash:'',
//         summary:'',
//       }
//   })
// })

//admin update
// app.get('/admin/update/:id',function(req,res) {
//   var id = req.params.id;
//   if(id){
//     Movie.findById(id,function(err,movie) {
//       res.render('admin',{
//         title:"immoc 后台更新页",
//         movie:movie
//       })
//     })
//   }
// })

//admin post movie
// app.post('/admin/movie/new',function(req,res) {
//   console.log('enter post');
//   var id = req.body.movie._id;
//   var movieObj =req.body.movie;
//   var _movie;
//   if(id !== 'undefined'){
//     Movie.findById(id,function(err,movie){
//       if(err){
//         console.log(err);
//       }
//       _movie = _.extend(movie,movieObj)
//       _movie.save(function(err,movie) {
//         if(err){
//           console.log(err);
//         }
//         res.redirect('/movie/'+movie._id);
//       })
//     })
//   }
//   else{
//     _movie = new Movie({
//       doctor:movieObj.doctor,
//       title:movieObj.title,
//       country:movieObj.country,
//       language:movieObj.language,
//       year:movieObj.year,
//       poster:movieObj.poster,
//       summary:movieObj.summary,
//       flash:movieObj.flash
//     })
//     _movie.save(function(err,movie) {
//       if(err){
//         console.log(err);
//       }
//       res.redirect('/movie/'+movie._id);
//     })
//   }
// })

//list
// app.get('/admin/list',(req,res)=>{
//   Movie.fetch(function(err,movies) {
//     if(err){
//       console.log(err);
//     }
//     res.render('list',{
//       title:'MOMO 列表页',
//       movies:movies
//     })
//   })
// })

//list delete
// app.delete('/admin/list',function(req,res) {
//   var id = req.query.id;
//   if(id) {
//     Movie.remove({_id: id},function(err,movie) {
//       if(err){
//         console.log(err);
//       }
//       else{
//         res.json({success: 1});
//       }
//     })
//   }
// })
